package com.icode.web.controller.house;

import com.icode.core.dto.about.*;
import com.icode.core.shared.DataToJson;
import com.icode.core.shared.HomeAboutHandle;
import com.icode.service.about.HomeAboutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-3-4
 * Time: 下午4:18
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/back/about")
public class HomeAboutController {

    @Autowired
    private HomeAboutService homeAboutService;

    @RequestMapping(value = "show", method = RequestMethod.GET)
    public String show() {
        return "back/about/about";
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public void form(String title, String data, String type, String id, HttpServletResponse response) {
        try {
            HomeAboutHandle.homeAboutForm(title, data, type, id, homeAboutService);
            DataToJson.DataToJson("success", response);
        } catch (Exception e) {
            e.printStackTrace();
            DataToJson.DataToJson("fail", response);
        }
    }

    @RequestMapping(value = "introduce", method = RequestMethod.GET)
    public ModelAndView introduce() {
        IntroduceDTO introduceDTO = homeAboutService.findIntroduce();
        if (introduceDTO == null) {
            return new ModelAndView("back/about/introduce", "introduceDTO", new IntroduceDTO());
        }
        return new ModelAndView("back/about/introduce", "introduceDTO", introduceDTO);
    }

    @RequestMapping(value = "model", method = RequestMethod.GET)
    public ModelAndView model() {
        ModelDTO modelDTO = homeAboutService.findModel();
        if (modelDTO == null) {
            return new ModelAndView("back/about/model", "modelDTO", new ModelDTO());
        }
        return new ModelAndView("back/about/model", "modelDTO", modelDTO);
    }

    @RequestMapping(value = "concept", method = RequestMethod.GET)
    public ModelAndView concept() {
        ConceptDTO conceptDTO = homeAboutService.findConcept();
        if (conceptDTO == null) {
            return new ModelAndView("back/about/concept", "conceptDTO", new ConceptDTO());
        }
        return new ModelAndView("back/about/concept", "conceptDTO", conceptDTO);
    }

    @RequestMapping(value = "news", method = RequestMethod.GET)
    public ModelAndView news() {
        List<NewsDTO> newsDTOs = homeAboutService.findNews();
        if (newsDTOs == null || newsDTOs.isEmpty()) {
            newsDTOs = new ArrayList<>();
            newsDTOs.add(new NewsDTO());
        }
        return new ModelAndView("back/about/news", "newsDTOs", newsDTOs);
    }

    @RequestMapping(value = "cooperation", method = RequestMethod.GET)
    public ModelAndView cooperation() {
        CooperationDTO cooperationDTO = homeAboutService.findCooperation();
        if (cooperationDTO == null) {
            return new ModelAndView("back/about/cooperation", "cooperationDTO", new CooperationDTO());
        }
        return new ModelAndView("back/about/cooperation", "cooperationDTO", cooperationDTO);
    }

    @RequestMapping(value = "news_manager", method = RequestMethod.GET)
    private String newsManager(Model model) {
        List<NewsDTO> newsDTOs = homeAboutService.findNews();
        if (newsDTOs == null || newsDTOs.isEmpty()) {
            newsDTOs = new ArrayList<>();
            newsDTOs.add(new NewsDTO());
        }
        model.addAttribute("newsDTOs", newsDTOs);
        return "back/about/newsManager";
    }

    @RequestMapping(value = "del_news", method = RequestMethod.POST)
    @ResponseBody
    private String delNews(String newsGuid) {
        homeAboutService.deleteNewsByGuid(newsGuid);
        return "success";
    }

}
